using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;

public partial class Admin_DiaDiemDanhSach : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDropDownList();
            LoadData();
        }
    }
    protected void LoadDropDownList()
    {
        tbDiaDiemCollection ddCollection = tbDiaDiemBUS.GetAll();
        ddl_madiadiem.DataTextField = tbDiaDiemColumns.MaDD.ToString();
        ddl_madiadiem.DataValueField = tbDiaDiemColumns.MaDD.ToString();
        ddl_madiadiem.DataSource = ddCollection;
        ddl_madiadiem.DataBind();
        ddl_madiadiem.Items.Insert(0, new ListItem("All", "0"));

        ddl_dienthoai.DataTextField = tbDiaDiemColumns.MaDD.ToString();
        ddl_dienthoai.DataValueField = tbDiaDiemColumns.DienThoai.ToString();
        ddl_dienthoai.DataSource = ddCollection;
        ddl_dienthoai.DataBind();
        ddl_dienthoai.Items.Insert(0, new ListItem("All", "0"));
        
    }
    private void LoadData()
    {
        Pager.PageSize = 5;
        Criteria c = new Criteria();

        if (ddl_madiadiem.SelectedValue != "0")
        {
            c.add(Condition.AND, tbDiaDiemColumns.MaDD, Condition.LIKE, int.Parse(ddl_madiadiem.SelectedValue));
        }
        if (txt_tendiadiem.Text  != "0")
        {
            c.add(Condition.AND, tbDiaDiemColumns.TenDD, Condition.LIKE,txt_tendiadiem.Text );
        }
        if (ddl_dienthoai.SelectedValue != "0")
        {
            c.add(Condition.AND, tbDiaDiemColumns.DienThoai, Condition.LIKE, int.Parse(ddl_dienthoai.SelectedValue));
        }

        int total = 0;
        tbDiaDiemCollection  ddCollection = tbDiaDiemBUS.Get(c.Criter, Pager.CurrentIndex, Pager.PageSize,
            tbDiaDiemColumns.MaDD.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = ddCollection;
        Repeater1.DataBind();
        Pager.ItemCount = total;
        if (total <= Pager.PageSize)
        {
            Pager.Visible = false;
        }
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            tbDiaDiem  diadiem = (tbDiaDiem )e.Item.DataItem;
            Literal lit_MaDD = (Literal)e.Item.FindControl("lit_MaDD");
            Literal lit_TenDD = (Literal)e.Item.FindControl("lit_TenDD");
            Literal lit_DiaChi = (Literal)e.Item.FindControl("lit_DiaChi");
            Literal lit_DienThoai = (Literal)e.Item.FindControl("lit_DienThoai");
            Image img_bando = (Image)e.Item.FindControl("img_bando");
            HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hpl_Edit");

            img_bando.ImageUrl = "~/Photos/Thumb/" + diadiem.BanDo;
            hpl_Edit.NavigateUrl = "~/Admin/DiaDiemUpdate.aspx?iD=" + diadiem.MaDD.ToString();
            
        }
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        LoadData();
    }

    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int iD = int.Parse(hf_id.Value);
                tbDiaDiem  diadiem = tbDiaDiemBUS.GetByID(iD);
                
                if (diadiem.BanDo != "no_img.jpg")
                {
                    System.IO.File.Delete(Server.MapPath("~/Photos/" + diadiem.BanDo));
                    System.IO.File.Delete(Server.MapPath("~/Photos/Thumb/" + diadiem.BanDo));
                }
                tbDiaDiemBUS.Delete(iD);
            }
        }
        Response.Redirect(Request.RawUrl);
    }
    protected void Pager_Command1(object sender, CommandEventArgs e)
    {
        Pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();
    }
}
